﻿using NPOI.HSSF.UserModel;
using OMG.Fill.Vegetable.Model.Ent;
using OMG.Fill.Vegetable.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Vegetable.Model.UI.Controllers
{
    public class VegProSurveyController : ApiController
    {
        VegProSurveyServices vegProSurveyServices = new VegProSurveyServices();

        //[ApiActionAuthorize("获取表3生产情况调查表T_VegProSurvey分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<VegProSurveyEntity>> PostPageList(VegProSurveyQuery query)
        {
            return vegProSurveyServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增表3生产情况调查表T_VegProSurvey指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(VegProSurveyEntity model)
        {
            return vegProSurveyServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改表3生产情况调查表T_VegProSurvey指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(List<VegProSurveyEntity> model)
        {
            return vegProSurveyServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除表3生产情况调查表T_VegProSurvey指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return vegProSurveyServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取表3生产情况调查表T_VegProSurvey指定对象操作")]
        //[ApiActionException]
        public ResResult<VegProSurveyEntity> Get(string id)
        {
            return vegProSurveyServices.GetModel(id);
        }
        /// <summary>
        /// 数据填报----更新报表数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ResResult<VegProSurveyEntity> PutUpdate(List<VegProSurveyEntity> list)
        {
            return vegProSurveyServices.UpdateModelList(list);
        }
        /// <summary>
        /// 导出下载响应(季度)
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelData(string ReportId, string FillInYear)
        {
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear + "(三)主要蔬菜生产情况季度调查.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegProSurveyServices.Method(ReportId).GetBuffer());
            curContext.Response.End();
        }
        /// <summary>
        /// 导出下载响应(年度)
        /// </summary>
        /// <param name="ReportCode"></param>
        public void GetExcelDataYear(string ReportId, string FillInYear)
        {
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = FillInYear + "(四)主要蔬菜播种面积、产量年度调查.xls";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegProSurveyServices.MethodYear(ReportId).GetBuffer());
            curContext.Response.End();
        }
        public ResResult<List<VegProSurveyStatsModel>> PostStats(VegNowModel model)
        {
            return vegProSurveyServices.GetStatsList(model);
        }
        //public ResResult<List<VegProSurveyStatsModel>> PostDetailStats(VegNowModel model)
        //{
        //    return vegProSurveyServices.GetDetailStats(model);
        //}
        public void GetStatsExcelData(string AreaCode, string FillInYear, string Status, string ReportCode, string TimesCycle, string ItemCode)
        {
            VegNowModel model = new VegNowModel();
            model.AreaCode = AreaCode;
            model.FillInYear = FillInYear;
            model.Status = Status;
            model.TimesCycle = TimesCycle;
            model.ReportCode = ReportCode;
            model.ItemCode = ItemCode;
            //打开完成excel文件并下载
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            string strFileName = model.FillInYear;
            if (ReportCode == "VegProSurvey")
            {
                strFileName += "（三）主要蔬菜生产情况季度调查表（全面统计）.xls";
            }
            else if (ReportCode == "VegProSurveyYear")
            {
                strFileName += "（四）主要蔬菜播种面积、产量年度调查表（全面统计）.xls";
            }
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(vegProSurveyServices.MethodStats(model).GetBuffer());
            curContext.Response.End();
        }

    }
}